/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl;

import cz.muni.fi.diplomka.datalayer.dao.ResearchDao;
import cz.muni.fi.diplomka.datalayer.dao.ResearchInProgressDao;
import cz.muni.fi.diplomka.datalayer.daoimpl.ResearchDaoImpl;
import cz.muni.fi.diplomka.datalayer.daoimpl.ResearchInProgressDaoImpl;
import cz.muni.fi.diplomka.datalayer.entity.ResearchEntity;
import cz.muni.fi.diplomka.datalayer.entity.ResearchInProgressEntity;
import cz.muni.fi.diplomka.servicelayer.dto.Research;
import cz.muni.fi.diplomka.servicelayer.dto.ResearchInProgress;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.ResearchManager;
import java.util.ArrayList;
import java.util.List;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;

/**
 *
 * @author Jakub Kutil
 */
public class ResearchManagerImpl implements ResearchManager {

    private ResearchInProgressDao researchInProgressDao = new ResearchInProgressDaoImpl();
    private ResearchDao researchDao = new ResearchDaoImpl();
    private Mapper mapper = new DozerBeanMapper();

    @Override
    public void startResearch(ResearchInProgress researchInProgress) {
       
        if (researchInProgress == null) {
            throw new IllegalArgumentException("Research In Progress can not be null.");
        }
        researchInProgressDao.addResearchInProgress(mapper.map(researchInProgress, ResearchInProgressEntity.class));
    }

    @Override
    public void finishResearch(ResearchInProgress researchInProgress) {

        if (researchInProgress == null) {
            throw new IllegalArgumentException("Research In Progress cant be null");
        }
        Research research = new Research();
        research.setPlayerId(researchInProgress.getPlayerId());
        research.setResearchType(researchInProgress.getResearchType());
        
        researchDao.addResearch(mapper.map(research, ResearchEntity.class));
        researchInProgressDao.deleteResearchInProgress(researchInProgress.getPlayerId(), researchInProgress.getResearchType());
        
    }

    @Override
    public List<ResearchInProgress> getAllResearchesInProgressOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID can not be null");
        }
        List<ResearchInProgress> list = new ArrayList<>();

        for (ResearchInProgressEntity r : researchInProgressDao.getAllResearchsInProgressForPlayer(playerId)) {
            list.add(mapper.map(r, ResearchInProgress.class));
        }
        return list;
    }

    @Override
    public List<Research> getAllFinishedResearchesOfPlayer(Long playerId) {
        if (playerId == null) {
            throw new IllegalArgumentException("Player ID can not be null");
        }
        List<Research> list = new ArrayList<>();

        for (ResearchEntity researchEntity : researchDao.getAllResearchesOfPlayer(playerId)) {
            list.add(mapper.map(researchEntity, Research.class));
        }
        return list;
    }
    
    @Override
    public List<ResearchInProgress> getAllResearchesInProgress() {
        List<ResearchInProgress> list = new ArrayList<>();

        for (ResearchInProgressEntity researchInProgressEntity : researchInProgressDao.getAllResearchsInProgress()) {
            list.add(mapper.map(researchInProgressEntity, ResearchInProgress.class));
        }
        return list;
    }
}
